
function metadata = getSImetadata(movname)

% newer version of scanimage has different .tif structure - this approach
% is much faster to read it but has to have all the associated functions on
% the path:
if isa(movname,'string')
    movname = convertStringsToChars(movname);
end
try 
    [header,mov,c] = scanimage.util.opentif(movname);
    metadata.zoom = header.SI.hRoiManager.scanZoomFactor;
    metadata.position = header.SI.hMotors.samplePosition;
    % as of 1/5/2023 new scanimage has a 1/2.5 conversion from the
    % "microns" it thinks and the microns it actually is. so all data from
    % the new scanimage (2022 or later) has to be scaled by that factor:
    if isa(c.si_ver,'double') && c.si_ver == 2022
        metadata.position = metadata.position*2.5;
        metadata.position = metadata.position([2,1,3]);
    end
    metadata.framerate = header.SI.hRoiManager.scanFrameRate;
    if length(header.SI.hChannels.channelSave) == 1
        metadata.frameaveraging = round(mean(diff(header.frameNumbers)));
    elseif length(header.SI.hChannels.channelSave) == 2
        temp = header.frameNumbers(1:2:end);
        metadata.frameaveraging = round(mean(diff(temp)));
    end
    if strcmp('loop',header.SI.acqState)
        metadata.numLoops = header.SI.acqsPerLoop;
    end
    if isfield(c,'numFrames')
        metadata.totalframes = c.numFrames*metadata.frameaveraging;
        metadata.Height = c.numLines;
        metadata.Width = c.numPixels;
    else
        metadata.totalframes = c.numImages;
        metadata.Height = size(mov,1);
        metadata.Width = size(mov,2);
    end
    
    metadata.savedchannels = header.SI.hChannels.channelSave;
    metadata.numZslices = header.SI.hStackManager.actualNumSlices;
    metadata.zStepSize = header.SI.hStackManager.actualStackZStepSize;
    return;
catch
    disp('unable to use post 2020 scanimage datatype')
end



t = Tiff(movname,'r');
iminfo = t.getTag('ImageDescription');

newlines = find(iminfo == newline);

% get zoom:
fieldname = 'scanZoomFactor';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.zoom = str2double(iminfo(startchar:endchar));

% get motor position:
fieldname = 'motorPosition';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
if isempty(startchar)
    metadata.position = nan(1,3);disp('unable to obtain position metadata')
else
    endchar = newlines - startchar; 
    endchar(endchar<0) = nan;
    endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
    position = eval(iminfo(startchar:endchar));
    % flip order of first and second columns to have x- and y- coordinates
    % appear as they are in the image that's presented:
    try
        metadata.position = [position(:,2),position(:,1),position(:,3)];
    catch
        metadata.position = nan(1,3);disp('unable to obtain position metadata')
    end
end
% get measured frame rate
fieldname = 'scanFrameRate';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.framerate = eval(iminfo(startchar:endchar));

% get averaging 
fieldname = 'frameNumberAcquisition';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.frameaveraging = eval(iminfo(startchar:endchar));

% get total number of loops if doing looping:
fieldname = 'acqsPerLoop';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.numLoops = eval(iminfo(startchar:endchar));

% run a patch that corrects for some issues that happen between looped and
% grabbed aquistion
fieldname = 'frameNumbers';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
temp = eval(iminfo(startchar:endchar));
if temp == 1
    %metadata.frameaveraging = metadata.numLoops;           %don't think this is correct?
    %metadata.numLoops = 1;
end
    
 

%get total number of frames;
fieldname = 'framesPerSlice';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.totalframes = eval(iminfo(startchar:endchar));

%get height/width
metadata.Height = t.getTag('ImageLength');
metadata.Width = t.getTag('ImageWidth');

%get number of channels to know if there is interleaving to deal with:
fieldname = 'channelSave';
temp = strfind(iminfo,fieldname);
startchar = temp + length(fieldname) + 2;
endchar = newlines - startchar; 
endchar(endchar<0) = nan;
endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
metadata.savedchannels = eval(iminfo(startchar:endchar));


%% if doing z-stack this will give the z stack info:
try
    fieldname = 'numSlices';
    temp = strfind(iminfo,fieldname);
    startchar = temp + length(fieldname) + 2;
    endchar = newlines - startchar; 
    endchar(endchar<0) = nan;
    endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
    metadata.numZslices = eval(iminfo(startchar:endchar));

    fieldname = 'stackZStepSize';
    temp = strfind(iminfo,fieldname);
    startchar = temp + length(fieldname) + 2;
    endchar = newlines - startchar; 
    endchar(endchar<0) = nan;
    endchar = find(endchar == min(endchar));endchar = newlines(endchar(1))-1;
    metadata.zStepSize = eval(iminfo(startchar:endchar));
catch
    disp('not z stack')
end


